package com.dbuil.bleshoes.utils.banner;

import android.app.Activity;
import android.content.Context;

import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;

import androidx.viewpager.widget.ViewPager;

import com.blankj.utilcode.util.SizeUtils;
import com.dbuil.bleshoes.R;
import com.dbuil.bleshoes.utils.glides.GlideUtils;
import com.stx.xhb.androidx.XBanner;


import java.util.ArrayList;
import java.util.List;

/**
 * Created buy 风凊扬 on 2019/9/19 0019
 */
public class XBannerUtils {


    public static void initGuideBanner(XBanner xbanner, Button mBtnEnter){


        List<LocalImageInfo> localImageInfoList=new ArrayList<>();
        localImageInfoList.add(new LocalImageInfo(R.drawable.guide));
        localImageInfoList.add(new LocalImageInfo(R.drawable.guide1));
        localImageInfoList.add(new LocalImageInfo(R.drawable.guide));
        xbanner.setBannerData(localImageInfoList);

        xbanner.loadImage((banner, model, view, position) -> ((ImageView) view).setImageResource(((LocalImageInfo) model).getXBannerUrl()));

        xbanner.setOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
                if (position == xbanner.getRealCount() - 1) {
                    mBtnEnter.setVisibility(View.VISIBLE);
                } else {
                    mBtnEnter.setVisibility(View.GONE);
                }
            }
        });


    }

    public static void initBanner(final Context context, XBanner xbanner, final List<BannerBean> data, final boolean isRoundcorner) {


        List<Bannerinfo> imageUrls = new ArrayList<>();
        for (int i = 0; i < data.size(); i++) {

            final String url = data.get(i).getCorver();
            if (!TextUtils.isEmpty(url)) {
                imageUrls.add(new Bannerinfo(url));
            }

        }


        xbanner.loadImage((banner, model, view, position) -> {

            ImageView bannerimg = view.findViewById(R.id.banner_img);

            if (isRoundcorner) {
                GlideUtils.loadRoundCornerImage(context, ((Bannerinfo)model).getXBannerUrl(), bannerimg, 6);
            } else {

                GlideUtils.loadImage(context, imageUrls.get(position).getXBannerUrl(), bannerimg);
            }


        });


        xbanner.setOnItemClickListener((banner, model, view, position) -> {

            BannerBean bannerBean = data.get(position);
            if (bannerItemClickListener!=null) {
                bannerItemClickListener.onBannerItemCLick(bannerBean,position);
            }

        });
        //xbanner.setBannerData(imageUrls);
        xbanner.setBannerData(R.layout.item_banner, imageUrls);


    }


    public static void indicatorBanner(Activity context, XBanner xbanner, List<BannerBean> data, boolean isRoundcorner, LinearLayout llPoints) {

        List<Bannerinfo> imageUrls = new ArrayList<>();
        llPoints.removeAllViews();
        for (int i = 0; i < data.size(); i++) {

            final String url = data.get(i).getCorver();
            if (!TextUtils.isEmpty(url)) {
                imageUrls.add(new Bannerinfo(url));
            }

            View pointview = LayoutInflater.from(context).inflate(R.layout.img_indicator, null);
            ImageView imageView = pointview.findViewById(R.id.img_points);
            RelativeLayout.LayoutParams layoutParams = (RelativeLayout.LayoutParams) imageView.getLayoutParams();
            if (i == 0) {
                layoutParams.width = SizeUtils.dp2px(18);
                layoutParams.height = SizeUtils.dp2px(5);
                imageView.setSelected(true);
            } else {
                layoutParams.width = SizeUtils.dp2px(5);
                layoutParams.height = SizeUtils.dp2px(5);
                imageView.setSelected(false);
            }

            imageView.setLayoutParams(layoutParams);
            llPoints.addView(pointview);

        }
        if (data.size() > 1) {
            llPoints.setVisibility(View.VISIBLE);
        } else {
            llPoints.setVisibility(View.GONE);
        }


        xbanner.loadImage((banner, model, view, position) -> {

            ImageView bannerimg = view.findViewById(R.id.banner_img);
            if (isRoundcorner) {
                GlideUtils.loadRoundCornerImage(context, ((Bannerinfo)model).getXBannerUrl(), bannerimg, 6);
                //GlideUtils.loadAvatar(context, imageUrls.get(position).getXBannerUrl(), bannerimg);
            } else {

                GlideUtils.loadImage(context, ((Bannerinfo)model).getXBannerUrl(), bannerimg);
            }

        });
        xbanner.setOnItemClickListener((banner, model, view, position) -> {

            BannerBean bannerBean = data.get(position);
            if (bannerItemClickListener!=null) {
                bannerItemClickListener.onBannerItemCLick(bannerBean,position);
            }

        });

        // xbanner.setData(R.layout.item_banner, imageUrls, null);
        xbanner.setBannerData(R.layout.item_banner, imageUrls);
       // xbanner.setBannerData(imageUrls);
        xbanner.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int i, float v, int i1) {

            }

            @Override
            public void onPageSelected(int i) {


                for (int i1 = 0; i1 < llPoints.getChildCount(); i1++) {
                    ImageView imageView = llPoints.getChildAt(i1).findViewById(R.id.img_points);
                    RelativeLayout.LayoutParams layoutParams = (RelativeLayout.LayoutParams) imageView.getLayoutParams();

                    if (i1 == i) {
                        //指示器选中状态
                        layoutParams.width = SizeUtils.dp2px(18);
                        layoutParams.height = SizeUtils.dp2px(5);
                        imageView.setSelected(true);

                    } else {
                        //指示器未选中状态
                        layoutParams.width = SizeUtils.dp2px(5);
                        layoutParams.height = SizeUtils.dp2px(5);
                        imageView.setSelected(false);
                    }

                    imageView.setLayoutParams(layoutParams);
                }
            }

            @Override
            public void onPageScrollStateChanged(int i) {

            }
        });

    }

    private static onBannerItemClickListener bannerItemClickListener;

    public static void setOnBannerItemClick(onBannerItemClickListener listener) {

      bannerItemClickListener = listener;
    }

    public interface onBannerItemClickListener {
        void onBannerItemCLick(BannerBean bannerBean,int position);
    }

}
